package com.hnkywl.sts.service.zjgl;

import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.zjgl.FycbDao;
import com.hnkywl.sts.entity.zjgl.Fycb;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class FycbService {
    private FycbDao fycbDao;

    private GsjgDao gsjgDao;

    /**
     * 保存科目代码
     *
     * @param fycb 科目代码实体
     * @return
     */
    @Transactional
    public void saveFycb(Fycb fycb) {
        this.fycbDao.save(fycb);
    }

//    @Transactional
//    public void createSjcode(Long gsId) {
//        this.fycbDao.createSjcode(gsId);
//    }
//
//    @Transactional
//    public void createSfxj(Long gsId) {
//        this.fycbDao.createSfxj(gsId);
//    }





    @Transactional
    public Fycb findFycbDetail(Long id) {
        return this.fycbDao.get(id);
    }


    /**
     * 查询费用成本
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllfycb( String nf,String yf,Long gsId,String gcMc,int pageNo, int pageSize) {
        Long nbgsIds = null;
        if(gsId !=null && gcMc.equals(gsjgDao.get(gsId).getJc())){
            nbgsIds = gsId;
        }
        Page<Fycb> FycbPage = this.fycbDao.findAllFycb( nf,yf,gcMc,nbgsIds,pageNo, pageSize);
        List<Fycb> FycbList = FycbPage.getResult() == null ? new ArrayList<Fycb>() : FycbPage.getResult();
        String FycbJson = BaseUtils.fetchCollectionToJson(FycbList, "id", "month", "gs.jc", "yhs", "yysj", "qtrf", "rfxj", "kkfy", "gdfy", "glfyxj", "gzfj","lwpq","rlzyxj");
        return "{\"total\":" + FycbPage.getTotalCount() + ",\"rows\":[" + FycbJson + "]}";
    }




//    public String findFycb(Long gsId, String code, int pageSize) {
//        Page<Fycb> page = fycbDao.findFycb(gsId, code, pageSize);
//        String json = BaseUtils.fetchCollectionToJson(page.getResult(), "id", "code", "name");
//        return "[" + json + "]";
//    }

        /**
     * 删除科目代码通过公司
     *
     * @param gsId 公司Id
     * @return
     */
    @Transactional
    public void deleteFycbBySj(String date) {
        List<Fycb> FycbList = this.fycbDao.findAllFycbByDate(date);
        if(FycbList.size()>0){
            for (Fycb Fycb : FycbList) {
                this.fycbDao.delete(Fycb);
            }
        }
    }


    @Autowired
    public void setFycbDao(FycbDao fycbDao) {
        this.fycbDao = fycbDao;
    }
    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
